
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.User;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Aksel Kvitberg
 */
public class AttackBot extends PircBot {

	ArrayList<String> actions = new ArrayList<String>();
	ArrayList<String> descriptions = new ArrayList<String>();
	ArrayList<String> items = new ArrayList<String>();
	Random random = new Random();

	public AttackBot() {
		actions = read("actions.txt");
		descriptions = read("descriptions.txt");
		items = read("items.txt");

		this.setName("AttackBot");
		this.setLogin("attackbot");
		this.setMessageDelay(3000);
		try {
			this.connect("irc.lessthan3.net");
			this.joinChannel("#zubac2");
		} catch (IOException ex) {
			Logger.getLogger(RegExBot.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IrcException ex) {
			Logger.getLogger(RegExBot.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public void onMessage(String channel, String sender, String login, String hostname, String message) {
		System.out.println(message);
		Pattern p = Pattern.compile("![Aa]ttack ([\\w-]+)");
		Matcher m = p.matcher(message);
		if (m.find()) {
			String user = m.group(1);
			if (isInChannel(channel, user)) {
				System.out.println("Found");
				attack(channel, user);
			}
		}
	}

	public void onJoin(String channel, String sender, String login, String hostname){
		super.onJoin(channel, sender, login, hostname);
		attack(channel, sender);
	}

	private void attack(String channel, String user) {
		String actionStr = actions.get(random.nextInt(actions.size()));
		String descriptionStr = descriptions.get(random.nextInt(descriptions.size()));

		Pattern usrPtrn = Pattern.compile("<user>");
		Pattern itmPtrn = Pattern.compile("<item>");

		Matcher usrMtch = usrPtrn.matcher(actionStr);
		actionStr = usrMtch.replaceAll(user);

		usrMtch = usrPtrn.matcher(descriptionStr);
		descriptionStr = usrMtch.replaceAll(user);

		Matcher itmMtch = itmPtrn.matcher(descriptionStr);
		if (itmMtch.find()) {
			String itemStr = items.get(random.nextInt(items.size()));
			Pattern namePtrn = Pattern.compile("<name>");
			Matcher m = namePtrn.matcher(itemStr);
			if (m.find()){
				m.replaceAll(user);
			}
			descriptionStr = itmMtch.replaceAll(itemStr);
		}
		String sendMessage = actionStr + " " + user + " " + descriptionStr;
		this.sendAction(channel, sendMessage);
//		sendMessage(channel, sendMessage);
	}

	private ArrayList<String> read(String filename) {
		ArrayList<String> s = new ArrayList<String>();
		try {
			BufferedReader br = null;
			File f = new File(filename);
			br = new BufferedReader(new FileReader(f));
			String line = null;
			while ((line = br.readLine()) != null) {
				s.add(line);
			}

		} catch (IOException ex) {
			Logger.getLogger(RegExBot.class.getName()).log(Level.SEVERE, null, ex);
		}
		return s;
	}

	public static void main(String args[]) {
		new AttackBot();
	}

	public void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason) {
		if (recipientNick.equalsIgnoreCase(getNick())) {
			joinChannel(channel);
		}
	}

	boolean isInChannel(String channel, String nick) {
		for (User user : this.getUsers(channel)) {
			System.out.println(user.getNick());
//			if (user.getNick().equals(nick)) {
			if (user.equals(nick) || user.equals("%" + nick)) {
				System.out.println("Nick " + nick + " is in channel " + channel);
				return true;
			}
		}
		System.out.println("Nick " + nick + " is not in channel " + channel);
		return false;
	}
}
